package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.BookOrderInfo;
import com.isunland.managesystem.entity.BookOrderInfoDetailOriginal;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.HashMap;
import org.json.JSONException;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/1/4 19:24
 */

public class BaseOrderDetailFragment extends BaseFragment {
  public static final String EXTRA_ITEM = "com.isunland.intelligentFarmbyWJ.EXTRA_ITEM";
  @BindView(R.id.tv_operationLocation) TextView tvOperationLocation;
  @BindView(R.id.tv_landArea) TextView tvLandArea;
  @BindView(R.id.tv_operationTime) TextView tvOperationTime;
  @BindView(R.id.tv_register) EditText tvRegister;
  @BindView(R.id.tv_registerTime) EditText tvRegisterTime;
  @BindView(R.id.tv_billStatus) EditText tvBillStatus;
  @BindView(R.id.tv_bookAccount) EditText tvBookAccount;
  @BindView(R.id.tv_contactPhone) EditText tvContactPhone;
  @BindView(R.id.tv_orderNo) EditText tvOrderNo;
  @BindView(R.id.tv_appointTaskDesc) TextView tvAppointTaskDesc;//作业描述
  @BindView(R.id.tv_regionX) EditText tvRegionX;//作业区域经度
  @BindView(R.id.tv_regionY) EditText tvRegionY;//作业区域维度
  @BindView(R.id.tv_bookDeposit) EditText tvBookDeposit;//预约保证金
  /**
   * 接单组织区域
   */
  @BindView(R.id.tv_cooperativeRegion) TextView tvCooperativeRegion;
  @BindView(R.id.tv_cooperativeMemberName) TextView tvCooperativeMemberName;
  @BindView(R.id.tv_confirmDate) TextView tvConfirmDate;
  @BindView(R.id.tv_contacts) EditText tvContacts;
  BookOrderInfo mItem;
  BaseVolleyActivity mActivity;

  //public static BaseOrderDetailFragment newInstance(BookOrderInfo item) {
  //    Bundle bundle = new Bundle();
  //    bundle.putSerializable(EXTRA_ITEM, item);
  //    BaseOrderDetailFragment fragment = new BaseOrderDetailFragment();
  //    fragment.setArguments(bundle);
  //    return fragment;
  //}

  //初始化数据
  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    mActivity = ((BaseVolleyActivity) getActivity());
    mItem = (BookOrderInfo) getArguments().getSerializable(EXTRA_ITEM);
  }

  //初始化界面
  @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_appointment_order_detail, container, false);
    ButterKnife.bind(this, v);
    return v;
  }

  //联网请求.
  @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    initView(mItem);
    volleyPostInit();
  }

  protected void volleyPostInit() {
    if (mItem == null || mItem.getId() == null) return;
    String mId = mItem.getId();

    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_GET_APPOINT_ORDER_DETAIL);
    HashMap<String, String> params = new HashMap<>();
    params.put("id", mId);

    MyUtils.showDialogLoading(mActivity);
    mActivity.volleyPost(url, params, new VolleyResponse() {
      @Override public void onVolleyError(VolleyError arg0) {
        ToastUtil.show("获取详情失败!");
      }

      @Override public void onVolleyResponse(String json) throws JSONException {
        //json = MyUtils.readAssetsFile(MyApplication.getAppContext(), "testOperationDetail.json");
        BookOrderInfoDetailOriginal original =
            new Gson().fromJson(json, BookOrderInfoDetailOriginal.class);
        if (original.getResult() == Base.SUCCESS && original.getData() != null) {
          mItem = original.getData();
          initView(original.getData());
        } else {
          ToastUtil.show("获取详情失败!");
        }
      }
    });
  }

  private void initView(BookOrderInfo data) {
    if (data == null) return;

    tvOrderNo.setText(data.getBookNo());
    tvAppointTaskDesc.setText(data.getBookWordDesc());
    //noinspection deprecation
    tvOperationTime.setText(
        MyDateUtil.parseDateToString(MyDateUtil.stringToDate(data.getBookWorkDate())));
    tvContacts.setText(data.getLinkmanName());
    tvContactPhone.setText(data.getLinkmanPhone());
    tvOperationLocation.setText(data.getWorkAddress());
    tvLandArea.setText(String.valueOf(data.getWorkArea()));
    tvRegister.setText(data.getRegStaffName());
    tvRegisterTime.setText(data.getRegDate());
    tvBillStatus.setText(data.getDataStatusName());
    tvBookAccount.setText(data.getBookAccountNo());
    tvRegionX.setText(data.getRegionX());
    tvRegionY.setText(data.getRegionY());
    tvBookDeposit.setText(data.getBookDeposit());
    tvCooperativeRegion.setText(data.getCooperativeRegion());
    tvCooperativeMemberName.setText(data.getCooperativeMemberName());
    tvConfirmDate.setText(data.getConfirmDate());
  }
}
